{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40\n",
      "40\n"
     ]
    }
   ],
   "source": [
    "# 全局变量\n",
    "a = 10\n",
    "\n",
    "def add(x,y):\n",
    "    # x y z 都是局部变量，作用范围函数内部\n",
    "    z = x+y+a\n",
    "\n",
    "    global z2\n",
    "    z2 = x+y+a\n",
    "\n",
    "    return z\n",
    "\n",
    "# 无法访问局部变量\n",
    "# print(x,y,z)\n",
    "print(add(10,20))\n",
    "print(z2)\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[40, 50, 60]\n",
      "[10, 20, 30]\n"
     ]
    }
   ],
   "source": [
    "# 全局变量\n",
    "ls = [10,20,30]\n",
    "\n",
    "def foo():\n",
    "    # ls 就是 局部变量\n",
    "    ls = [40,50,60]\n",
    "    print(ls)\n",
    "\n",
    "foo()\n",
    "print(ls)   \n",
    " \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 全局变量\n",
    "ls = [10,20,30]\n",
    "\n",
    "def foo():\n",
    "    # ls 就是 局部变量\n",
    "    #ls = [40,50,60]\n",
    "    # ls 是访问的是全局变量\n",
    "    ls[0] = 90\n",
    "    print(ls)\n",
    "\n",
    "foo()\n",
    "print(ls)   "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
